#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.db import models


class Type(models.Model):
    '''设备类型，对应flag编号1'''
    num = models.CharField(verbose_name='分类号', max_length=10)
    name = models.CharField(verbose_name='分类名称', max_length=50)
    manufacturer = models.CharField(verbose_name='生产厂家', max_length=50)
    pattern = models.CharField(verbose_name='型号', max_length=30)
    standard = models.CharField(verbose_name='规格', max_length=100)

    def __unicode__(self):
        return self.name

    def phone(self):
        '''对手机返回，由非外键字段的(verbose_name:字段值)组成的字典'''
        info = {}
        li = self.__dict__.keys()
        li.remove('id')
        li.remove('_state')
        for i in li:
            info[self._meta.get_field(i).verbose_name.decode("utf-8")] = self.__dict__[i]
        return info

    class Meta:
        '''定义中文名'''
        verbose_name = '类型'
        verbose_name_plural = '按类型查看'



class Device(models.Model):
    '''设备基本信息,对应flag编号0'''
    num = models.CharField(verbose_name='设备编号', max_length='15')
    name = models.CharField(verbose_name='设备名称', max_length='20')
    price = models.DecimalField(verbose_name='单价', max_digits=8, decimal_places=2)
    cost = models.DecimalField(verbose_name='总造价', max_digits=8, decimal_places=2)
    location = models.CharField(verbose_name='存放地点', max_length=20)
    man = models.CharField(verbose_name='使（领）用人', max_length=10, blank=True)
    status = models.CharField(verbose_name='现状', max_length=4, blank=True)
    unit = models.CharField(verbose_name='计量单位', max_length=4, blank=True)
    date_of_purchase = models.DateField(verbose_name='购置日期')
    type = models.ForeignKey(Type, verbose_name='设备类型')

    def __unicode__(self):
        return self.num+u' '+self.name

    def phone(self):
        '''手机查询用，由非外键字段的(verbose_name:字段值)组成的字典'''
        info = {}
        dic = {
            'num':self.num,
            'name':self.name,
            'price':self.price,
            'cost':self.cost,
            'location':self.location,
            'status':self.status,
            'unit':self.unit,
            'man':self.man,
            'date_of_purchase':self.date_of_purchase,
        }
        li = dic.keys()
        for i in li:
            info[self._meta.get_field(i).verbose_name.decode("utf-8")] = self.__dict__[i]
        return info

    def unqrcode(self):
        '''待二维码化的内容'''
        return self.num+';'+self.name+';'

    class Meta:
        verbose_name = '设备'
        verbose_name_plural = '按编号查看'
